package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacCultureCombinaRel;
import com.ysd.lis.entity.bac.BacCultureCombination;
import com.ysd.lis.entity.bac.BacCultureMedium;
import com.ysd.lis.entity.sta.StaCultureCombinaRel;
import com.ysd.lis.entity.sta.StaCultureCombination;
import com.ysd.lis.mapper.bac.BacCultureCombinaRelMapper;
import com.ysd.lis.mapper.bac.BacCultureCombinationMapper;
import com.ysd.lis.mapper.bac.BacCultureMediumMapper;
import com.ysd.lis.mapper.sta.StaCultureCombinaRelMapper;
import com.ysd.lis.mapper.sta.StaCultureCombinationMapper;
import com.ysd.lis.request.BacCultureCombinationDto;
import com.ysd.lis.request.Params;
import com.ysd.lis.service.bac.BacCultureCombinationService;
import com.ysd.lis.service.bac.BacCultureMediumService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bai
 * @since 2024-04-10
 */
@Service
public class BacCultureCombinationServiceImpl extends ServiceImpl<BacCultureCombinationMapper, BacCultureCombination> implements BacCultureCombinationService {
    @Autowired
    BacCultureCombinationMapper bacCultureCombinationMapper;
    @Autowired
    BacCultureCombinaRelMapper bacCultureCombinaRelMapper;
    @Autowired
    BacCultureMediumMapper bacCultureMediumMapper;
    @Autowired
    BacCultureMediumService bacCultureMediumService;

    @Autowired
    StaCultureCombinationMapper staCultureCombinationMapper;
    @Autowired
    StaCultureCombinaRelMapper staCultureCombinaRelMapper;

    @Override
    public List<BacCultureCombination> findBacCultureCombinationList(BacCultureCombination bacCultureCombination) {
        LambdaUpdateWrapper<BacCultureCombination> queryWrapper = new LambdaUpdateWrapper<>();
        if (!StringUtils.isEmpty(bacCultureCombination.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacCultureCombination::getCode, bacCultureCombination.getSearchValue()).or().like(BacCultureCombination::getName, bacCultureCombination.getSearchValue()).or().like(BacCultureCombination::getInputCode, bacCultureCombination.getSearchValue()));
        }
        queryWrapper.eq(StringUtils.isNotBlank(bacCultureCombination.getType()), BacCultureCombination::getType, bacCultureCombination.getType());
        List<BacCultureCombination> bacCultureCombinations = bacCultureCombinationMapper.selectList(queryWrapper);

        List<String> nos = bacCultureCombinations.stream().map(BacCultureCombination::getCode).collect(Collectors.toList());

        if (nos.isEmpty()) {
            return bacCultureCombinations;
        }

        MPJLambdaWrapper<BacCultureCombinaRel> lambdaUpdateWrapper = new MPJLambdaWrapper<>();
        lambdaUpdateWrapper.selectAll(BacCultureCombinaRel.class);
        lambdaUpdateWrapper.selectAs("culmdm", BacCultureMedium::getName, BacCultureCombinaRel::getCultrueName);
        lambdaUpdateWrapper.eq(BacCultureCombinaRel::getDelFlag, 0);
        lambdaUpdateWrapper.in(BacCultureCombinaRel::getCulCombinaNo, nos);
        lambdaUpdateWrapper.leftJoin(BacCultureMedium.class, "culmdm", p -> p.eq(BacCultureMedium::getCode, BacCultureCombinaRel::getCulMediumNo).eq(BacCultureMedium::getDelFlag, 0));
        lambdaUpdateWrapper.orderByAsc(BacCultureCombinaRel::getOrderNum);
        List<BacCultureCombinaRel> bacCultureCombinaRels = bacCultureCombinaRelMapper.selectList(lambdaUpdateWrapper);

        bacCultureCombinations.forEach(a -> {
//            LambdaUpdateWrapper<BacCultureCombinaRel> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//            lambdaUpdateWrapper.eq(BacCultureCombinaRel::getCulCombinaNo, a.getCode());
//            lambdaUpdateWrapper.orderByAsc(BacCultureCombinaRel::getOrderNum);
//            List<BacCultureCombinaRel> bacCultureCombinaRels = bacCultureCombinaRelMapper.selectList(lambdaUpdateWrapper);

            List<BacCultureCombinaRel> lst = bacCultureCombinaRels.stream().filter(p -> StringUtils.isNotBlank(p.getCulCombinaNo()) && p.getCulCombinaNo().equals(a.getCode())).collect(Collectors.toList());

//            bacCultureCombinaRels.forEach(b -> {
//                if (StringUtils.isNotBlank(b.getCulMediumNo())) {
//                    BacCultureMedium bacCultureMedium = bacCultureMediumService.findFirstItemByNo(b.getCulMediumNo());
//                    b.setCultrueName(bacCultureMedium == null ? "" : bacCultureMedium.getName());
//                }
//                //BacCultureMedium bacCultureMedium = bacCultureMediumMapper.selectById(b.getCultrueId());
//                //b.setCultrueName(bacCultureMedium == null ? "" : bacCultureMedium.getName());
//            });
            if (CollectionUtils.isNotEmpty(lst)) {
                a.setCultureRelList(lst);
            }
        });

        return bacCultureCombinations;
    }

    @Override
    public Result addBacCultureCombination(BacCultureCombination BacCultureCombination) {
        int insert = bacCultureCombinationMapper.insert(BacCultureCombination);
        if (CollectionUtils.isNotEmpty(BacCultureCombination.getCultureRelList())) {
            BacCultureCombination.getCultureRelList().forEach(a -> {
                //a.setCombinationId(BacCultureCombination.getId());
                a.setCulCombinaNo(BacCultureCombination.getCode());
                bacCultureCombinaRelMapper.insert(a);
            });
        }
        return Result.succ(insert);
    }

    @Override
    public Result deleteBacCultureCombination(BacCultureCombinationDto bacCultureCombinationDto) {
        List<BacCultureCombination> bacCultureCombinations = bacCultureCombinationDto.getBacCultureCombinations();
        List<String> ids = bacCultureCombinations.stream().map(BacCultureCombination::getId).collect(Collectors.toList());
        List<String> Nos = bacCultureCombinations.stream().map(BacCultureCombination::getCode).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            LambdaQueryWrapper<BacCultureCombinaRel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BacCultureCombinaRel::getDelFlag, 0);
            lambdaQueryWrapper.in(BacCultureCombinaRel::getCulCombinaNo, Nos);
            bacCultureCombinaRelMapper.delete(lambdaQueryWrapper);
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result updateBacCultureCombination(BacCultureCombination bacCultureCombination) {
        int i = bacCultureCombinationMapper.updateById(bacCultureCombination);
        LambdaUpdateWrapper<BacCultureCombinaRel> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(BacCultureCombinaRel::getCulCombinaNo, bacCultureCombination.getCode());
        bacCultureCombinaRelMapper.delete(lambdaUpdateWrapper);
        if (CollectionUtils.isNotEmpty(bacCultureCombination.getCultureRelList())) {
            bacCultureCombination.getCultureRelList().forEach(a -> {
                a.setCulCombinaNo(bacCultureCombination.getCode());
                bacCultureCombinaRelMapper.insert(a);
            });
        }
        return Result.succ(i);
    }

    @Override
    public Result findBacCultureCombinationListByType(Params params) {
        LambdaQueryWrapper<BacCultureCombination> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacCultureCombination::getDelFlag, 0).eq(BacCultureCombination::getType, params.getType());
        List<BacCultureCombination> bacCultureCombinationList = bacCultureCombinationMapper.selectList(queryWrapper);
        return Result.fail(1, "查询成功", bacCultureCombinationList);
    }

    /*
     * param @id 培养基组合编号
     * */
    @Override
    public Result findBacCultureCombinationListById(String id) {

        MPJLambdaWrapper<BacCultureMedium> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(BacCultureMedium.class);
        mpjLambdaWrapper.leftJoin(BacCultureCombinaRel.class, p -> p.eq(BacCultureCombinaRel::getCulMediumNo, BacCultureMedium::getCode).eq(BacCultureCombinaRel::getDelFlag, 0));
        mpjLambdaWrapper.eq(BacCultureMedium::getDelFlag, 0);
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(id)) {
            mpjLambdaWrapper.eq(BacCultureCombinaRel::getCulCombinaNo, id);
        }
        List<BacCultureMedium> bacCultureMediumList = bacCultureMediumMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", bacCultureMediumList);

    }

    /*
     * param @idList 培养基组合编号集合
     * */
    @Override
    public Result findBacCultureCombinationListByIdList(List<String> idList) {
        LambdaQueryWrapper<BacCultureCombinaRel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacCultureCombinaRel::getDelFlag, 0);
        queryWrapper.in(BacCultureCombinaRel::getCulCombinaNo, idList);
        List<BacCultureCombinaRel> bacCultureCombinaRels = bacCultureCombinaRelMapper.selectList(queryWrapper);
        return Result.fail(1, "查询成功", bacCultureCombinaRels);

    }

    @Override
    public Result findBacCultureCombinationListByIds(List<String> ids) {
        if (ids.size() < 1) {
            return Result.fail("参数不允许为空");
        }

        MPJLambdaWrapper<BacCultureMedium> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(BacCultureMedium.class);
        mpjLambdaWrapper.leftJoin(BacCultureCombinaRel.class, p -> p.eq(BacCultureCombinaRel::getCulMediumNo, BacCultureMedium::getCode).eq(BacCultureCombinaRel::getDelFlag, 0));
        mpjLambdaWrapper.eq(BacCultureMedium::getDelFlag, 0);
        if (!ids.isEmpty()) {
            mpjLambdaWrapper.in(BacCultureCombinaRel::getCulCombinaNo, ids);
        } else {
            mpjLambdaWrapper.in(BacCultureCombinaRel::getCulCombinaNo, (new ArrayList<>()).add("!@#QWE"));
        }

        List<BacCultureMedium> bacCultureMediumList = bacCultureMediumMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", bacCultureMediumList);
    }

    /*根据编号批量删除*/
    @Override
    public Boolean removeByNos(List<String> nos) {
        LambdaQueryWrapper<BacCultureCombination> deleteWapper = new LambdaQueryWrapper<>();
        deleteWapper.eq(BacCultureCombination::getDelFlag, 0);
        deleteWapper.in(BacCultureCombination::getCode, nos);
        //editWapper.set(BacCultureCombination::getDelFlag, 1);
        List<BacCultureCombination> lst = bacCultureCombinationMapper.selectList(deleteWapper);
        List<String> ids = lst.isEmpty() ? new ArrayList<>() : lst.stream().map(BacCultureCombination::getId).collect(Collectors.toList());
        if (ids.isEmpty()) {
            return false;
        }
        return bacCultureCombinaRelMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Result updateCultureCombinationListByIds(List<String> ids) {
        if (ids.size() < 1) {
            return Result.fail("参数不允许为空");
        }
        //根据ids查出数据
        LambdaQueryWrapper<StaCultureCombination> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StaCultureCombination::getDelFlag, 0);
        lambdaQueryWrapper.in(StaCultureCombination::getCode, ids);
        List<StaCultureCombination> bacCultureCombinationList = staCultureCombinationMapper.selectList(lambdaQueryWrapper);
        //收集编号
        List<String> codes = bacCultureCombinationList.stream().map(StaCultureCombination::getCode).collect(Collectors.toList());

        //根据编号查询出数据
        LambdaQueryWrapper<StaCultureCombinaRel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaCultureCombinaRel::getDelFlag, 0);
        queryWrapper.in(StaCultureCombinaRel::getCulCombinaNo, codes);
        List<StaCultureCombinaRel> bacCultureCombinaRels = staCultureCombinaRelMapper.selectList(queryWrapper);
        //把bacCultureCombinationList和bacCultureCombinaRels 分别批量插入  BacCultureCombination 和 BacCultureCombinaRel

        List<BacCultureCombination> bacCultureCombinations = new ArrayList<>();

        for (StaCultureCombination cultureCombination : bacCultureCombinationList){
            BacCultureCombination bacCultureCombination = new BacCultureCombination();
            BeanUtil.copyProperties(cultureCombination, bacCultureCombination,"id");
            bacCultureCombinations.add(bacCultureCombination);
        }
        this.saveOrUpdateBatch(bacCultureCombinations);

        for (StaCultureCombinaRel cultureCombinaRel : bacCultureCombinaRels){
            BacCultureCombinaRel bacCultureCombinaRel = new BacCultureCombinaRel();
            BeanUtil.copyProperties(cultureCombinaRel, bacCultureCombinaRel,"id");
            bacCultureCombinaRelMapper.insert(bacCultureCombinaRel);
        }

        return Result.succ(1, "更新成功", null);
    }
}
